package com.dragon.www.haier.net;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.Log;


import com.dragon.www.haier.model.xml.CodeXmlRootHandler;
import com.dragon.www.haier.model.xml.Dictionary;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * 资源工具类
 * Created by Administrator on 2015/8/28 16:17.
 * Author: Songxy
 * Email: thinkdata@163.com
 */
public class ResourceUtil {

    final static String TAG = "ResourceUtil";

    public static Map<String, String> getLayoutMappingsResource(Context context, int resourceId)
            throws IOException {
        Map<String, String> result = null;
        String key = null, value = null;
        try {
            XmlResourceParser parser = checkXml(context, resourceId);
            int eventType = parser.getEventType();
            while (eventType != XmlResourceParser.END_DOCUMENT) {
                if (eventType == XmlResourceParser.START_DOCUMENT) {
                    Log.d(TAG, "Start Document");
                } else if (eventType == XmlResourceParser.END_TAG) {
                    if (parser.getName().equals("entry")) {
                        result.put(key, value);
                        key = null;
                        value = null;
                    }
                } else if (eventType == XmlResourceParser.START_TAG) {
                    if (parser.getName().equals("map")) {
                        boolean isLinked = Boolean.parseBoolean(parser.getAttributeValue(null, "linked"));
                        result = isLinked ? new LinkedHashMap() : new HashMap();
                    } else if (parser.getName().equals("entry")) {
                        key = context.getResources().getString(context.getResources().getIdentifier(
                                parser.getAttributeValue(0), "string", context.getPackageName()));

                    }
                } else if (eventType == XmlResourceParser.TEXT) {
                    if (key != null) {
                        value = parser.getText();
                    }
                }
                eventType = parser.next();
            }
        } catch (Resources.NotFoundException e) {
            throw new IOException("Resource is Not Found");
        } catch (XmlPullParserException e) {
            throw new IOException("Resource Xml Pull Parser Error");
        }
        return result;
    }

    public static List<Dictionary> getDictionary(Context context, int resourceId) throws
            ParserConfigurationException, IOException, SAXException {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser1 = saxParserFactory.newSAXParser();
        CodeXmlRootHandler handler = new CodeXmlRootHandler();
        parser1.parse(context.getResources().openRawResource(resourceId), handler);
        return handler.getNode();
    }

    private static XmlResourceParser checkXml(Context context, int resourceId)
            throws IOException {
        XmlResourceParser parser = null;
        parser = context.getResources().getXml(resourceId);
        if (parser == null) throw new IOException("File Not Found");
        return parser;
    }
}
